package com.hq.ims.logincenter.controller;

import com.hq.ims.data.entity.Company;
import com.hq.ims.data.service.BaseCompanyService;
import com.hq.ims.data.utils.LocalConstant;
import com.hq.ims.data.vo.CompanyMapstruct;
import com.hq.ims.data.vo.UserMapstruct;
import com.hq.ims.logincenter.service.LoginCenterBusService;
import com.hq.utils.vo.ReturnVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 登录中心的controller
 */
@Api("注册中心")
@RestController
@RequestMapping(value = LocalConstant.SYS_TYPE_PUBLIC_URL_PREFIX + "/register")
public class RegisterController {

    @Resource
    private LoginCenterBusService loginCenterService;
    @Resource
    private BaseCompanyService baseCompanyService;

    @ApiOperation(value = "手机号注册", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/mobileRegister", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> mobileRegister(@RequestBody UserMapstruct.MobileRegisterVo mobileRegisterVo) {
        loginCenterService.mobileRegister(mobileRegisterVo);
        return ReturnVo.buildSuccess(true);
    }

    @ApiOperation(value = "手机号注册(一体化注册)", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/userAndCompanyRegister", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> userAndCompanyRegister(@RequestBody UserMapstruct.UserCompanyRegisterSaveVo userCompanyRegisterSaveVo) {
        loginCenterService.userAndCompanyRegister(userCompanyRegisterSaveVo);
        return ReturnVo.buildSuccess(true);
    }

    @ApiOperation(value = "注册获取所有公司", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/registerGetCompanyList", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<List<CompanyMapstruct.CompanyTreeVo>> registerGetCompanyList() {
        List<Company> redisAllCompanyList = baseCompanyService.lambdaQuery()
                .select(
                        Company::getCompanyId,
                        Company::getCompanyName
                )
                .orderByAsc(Company::getCompanyLevel)
                .list();
        List<CompanyMapstruct.CompanyTreeVo> companyTreeVoList = CompanyMapstruct.INSTANCE.toCompanyTreeVoList(redisAllCompanyList);
        return ReturnVo.buildSuccess(companyTreeVoList);
    }

    @ApiOperation(value = "找回密码", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/forgotPassword", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> forgotPassword(
            @RequestBody UserMapstruct.ForgotPasswordVo forgotPasswordVo
    ) {
        loginCenterService.forgotPassword(forgotPasswordVo);
        return ReturnVo.buildSuccess(true);
    }

}
